#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>


#if 0
void thread1_clean(void *arg)
{
	printf("%s\r\n", (char*)arg);
}

void *thread1(void *arg)
{
	int restul;
	
	//设置线程清理函数
	pthread_cleanup_push(thread1_clean, (void*)"thread1_clean");
	
	printf("thread enter\r\n");
	
	//设置线程取消状态，默认是可以取消的
	restul = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
//	restul = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	if(restul != 0)
	{
		printf("pthread_create: error code %d\r\n", restul);
		return "error";
	}
	//设置取消方式，默认是运行至下一个取消点再结束
	restul = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
	if(restul != 0)
	{
		printf("pthread_create: error code %d\r\n", restul);
		return "error";
	}
	
	sleep(1);
	sleep(3);
	printf("thread exit\r\n");

	//执行线程清理函数
	pthread_cleanup_pop(0);
//	return "succeed";
	pthread_exit("succeed");
}

int main(int argv, char *argc[])
{
	int restul;
	void *exit_value;
	pthread_t tid1; 
	
	//创建线程
	restul = pthread_create(&tid1, NULL, thread1, "this thread1");
	if(restul != 0)
	{
		printf("pthread_create: error code %d\r\n", restul);
		return -1;
	}
	
	//脱离线程，脱离状态的线程由系统自动回收资源，不能使用pthread_join回收
	restul = pthread_detach(tid1);
	if(restul != 0)
	{
		printf("pthread_detach: error code %d\r\n", restul);
		return -1;
	}
	
	sleep(2);
	//取消线程
	restul = pthread_cancel(tid1);
	if(restul != 0)
	{
		printf("pthread_cancel: error code %d\r\n", restul);
		return -1;
	}
	
	//回收线程资源，如果线程被提前取消则线程返回值为(void*)-1
	restul = pthread_join(tid1, &exit_value);
	if(restul != 0)
	{
		printf("pthread_join: error code %d\r\n", restul);
		return -1;
	}
	if(exit_value != (void*)-1)
		printf("exit_value = %s\r\n", (char*)exit_value);
	else
		printf("exit_value = %p\r\n", exit_value);
	
	//主线程不能退出
	while(1)
	{
		sleep(10);
	}
}
#else

void *thread1(void *arg)
{
	int restul;

	printf("thread enter\r\n");
	
	sleep(3);
	printf("thread exit\r\n");

	pthread_exit("succeed");
}

int main(void)
{
	int restul;
	void *exit_value;
	pthread_t tid1; 
	pthread_attr_t attr;
	
	//初始化线程属性
	restul = pthread_attr_init(&attr);
	if(restul != 0)
	{
		printf("pthread_attr_init: error code %d\r\n", restul);
		return -1;
	}
	//设置堆栈大小
	restul = pthread_attr_setstacksize(&attr, 1024*1024);
	if(restul != 0)
	{
		printf("pthread_attr_setstacksize: error code %d\r\n", restul);
		return -1;
	}
	//设置分离状态
	restul = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
//	restul = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if(restul != 0)
	{
		printf("pthread_attr_setdetachstate: error code %d\r\n", restul);
		return -1;
	}
	//创建线程
	restul = pthread_create(&tid1, &attr, thread1, "this thread1");
	if(restul != 0)
	{
		printf("pthread_create: error code %d\r\n", restul);
		return -1;
	}
	
//	sleep(2);
//	//取消线程
//	restul = pthread_cancel(tid1);
//	if(restul != 0)
//	{
//		printf("pthread_cancel: error code %d\r\n", restul);
//		return -1;
//	}
	
	//回收线程资源，如果线程被提前取消则线程返回值为(void*)-1
	restul = pthread_join(tid1, &exit_value);
	if(restul != 0)
	{
		printf("pthread_join: error code %d\r\n", restul);
		return -1;
	}
	if(exit_value != (void*)-1)
		printf("exit_value = %s\r\n", (char*)exit_value);
	else
		printf("exit_value = %p\r\n", exit_value);
	
	while(1)
	{
		sleep(10);
	}
}
#endif